package com.cyyz.service.Impl;

import com.cyyz.NotFoundException;
import com.cyyz.dao.CommentDao;
import com.cyyz.dao.TagDao;
import com.cyyz.entity.BlogAndTag;
import com.cyyz.entity.Tag;
import com.cyyz.service.CommentService;
import com.cyyz.util.MarkdownUtils;
import com.cyyz.dao.BlogDao;
import com.cyyz.entity.Blog;
import com.cyyz.queryvo.*;
import com.cyyz.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 博客列表业务层接口实现类
 * @Author: ONESTAR
 * @Date: Created in 23:36 2020/3/30
 * @QQ群: 530311074
 * @URL: https://onestar.newstar.net.cn/
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogDao blogDao;
    @Autowired
    private TagDao tagDao;
    @Autowired
    private CommentDao commentDao;

    @Override
    public ShowBlog getBlogById(Long id) {
        return blogDao.getBlogById(id);
    }

    @Override
    public List<BlogQuery> getAllBlog() {
        return blogDao.getAllBlogQuery();
    }

    @Transactional
    @Override
    public int saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        //保存博客
        blogDao.saveBlog(blog);
        //保存博客后才能获取自增的id
        Long id = blog.getId();
        //将标签的数据存到t_blogs_tag表中
        List<Tag> tags = blog.getTags();
        BlogAndTag blogAndTag = null;
        if (tags!=null)
            for (Tag tag : tags) {
                //新增时无法获取自增的id,在mybatis里修改
                blogAndTag = new BlogAndTag();
                blogAndTag.setBlogId(id);
                blogAndTag.setTagId(tag.getId());
                blogDao.saveBlogAndTag(blogAndTag);
            }
        return 1;
    }

    @Transactional
    @Override
    public int updateBlog(ShowBlog showBlog) {
        showBlog.setUpdateTime(new Date());
        int updateBlog = blogDao.updateBlog(showBlog);
        blogDao.deleteAllBlogTags(showBlog.getId());
        BlogAndTag blogAndTag = null;
        //将标签的数据存到t_blogs_tag表中
        List<Tag> tags = showBlog.getTags();
//        System.out.println(tags);
//        System.out.println(showBlog.getTagIds());
        if (tags!=null)
            for (Tag tag : tags) {
                blogAndTag = new BlogAndTag();
                //新增时无法获取自增的id,在mybatis里修改
                blogAndTag.setBlogId(showBlog.getId());
                blogAndTag.setTagId(tag.getId());
                blogDao.saveBlogAndTag(blogAndTag);
            }
        return 1;
    }
    @Transactional
    @Override
    public void deleteBlog(Long id) {
        blogDao.deleteBlog(id);
        blogDao.deleteAllBlogTags(id);
        commentDao.deleteCommentByBlogId(id);
    }

    @Override
    public List<BlogQuery> getBlogBySearch(SearchBlog searchBlog) {
        return blogDao.searchByTitleOrTypeOrRecommend(searchBlog);
    }

    @Override
    public List<FirstPageBlog> getByTagId(Long tagId) {
        return blogDao.getByTagId(tagId);
    }

    @Override
    public List<FirstPageBlog> getAllFirstPageBlog() {
        return blogDao.getFirstPageBlog();
    }

    @Override
    public List<RecommendBlog> getRecommendedBlog() {
        List<RecommendBlog> allRecommendBlog = blogDao.getAllRecommendBlog();
        return allRecommendBlog;
    }

//    @Override
//    public List<FirstPageBlog> getNewBlog() {
//        return blogDao.getNewBlog();
//    }

    @Override
    public List<FirstPageBlog> getSearchBlog(String query) {
        return blogDao.getSearchBlog(query);
    }

    @Override
    public DetailedBlog getDetailedBlog(Long id) {
        System.out.println(id);
        DetailedBlog detailedBlog = blogDao.getDetailedBlog(id);
        if (detailedBlog == null) {
            throw new NotFoundException("该博客不存在");
        }
        String content = detailedBlog.getContent();
        detailedBlog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
//        文章访问数量自增
        blogDao.updateViews(id);
//        文章评论数量更新
        blogDao.getCommentCountById(id);
        return detailedBlog;
    }


    @Override
    public List<FirstPageBlog> getByTypeId(Long typeId) {
        return blogDao.getByTypeId(typeId);
    }

    @Override
    public Integer getBlogTotal() {
        return blogDao.getBlogTotal();
    }

    @Override
    public Integer getBlogViewTotal() {
        return blogDao.getBlogViewTotal();
    }

    @Override
    public Integer getBlogCommentTotal() {
        return blogDao.getBlogCommentTotal();
    }

    @Override
    public Integer getBlogMessageTotal() {
        return blogDao.getBlogMessageTotal();
    }
}